<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="公网访问内网机器：基于frp的内网穿透, python,machine learning,deep learning,html,css,c,c++,cpp,cmake,ros,linux,ubuntu">
    <meta name="description" content="本文介绍了如何利用frp实现公网访问内网机器">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>公网访问内网机器：基于frp的内网穿透 | JackWang&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.2">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size: 100% 100%;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">JackWang&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-book-reader" style="zoom: 0.6;"></i>
      
      <span>博客</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按标签归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按目录归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按日期分类文章</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>



<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">JackWang&#39;s Blog</div>
        <div class="logo-desc">
            
            JackWang的个人博客
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book-reader"></i>
			
			博客
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
                              <span>按标签归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
                              <span>按目录归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
                              <span>按日期分类文章</    span>

                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('抱歉，这篇文章并不想让所有人都看到，请输入授权密码观看')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717163415304.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">公网访问内网机器：基于frp的内网穿透</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">
                                <span class="chip bg-color">内网穿透</span>
                            </a>
                        
                            <a href="/tags/frp/">
                                <span class="chip bg-color">frp</span>
                            </a>
                        
                            <a href="/tags/Linux%E5%AE%9E%E7%94%A8%E6%8A%80%E5%B7%A7/">
                                <span class="chip bg-color">Linux实用技巧</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Linux%E5%AE%9E%E7%94%A8%E6%8A%80%E5%B7%A7/" class="post-category">
                                Linux实用技巧
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2023-07-17
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-07-20
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    4.7k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    17 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <blockquote>
<p>因为经常需要远程访问自己的机器，所以写一个博客记录一下</p>
</blockquote>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717163415304.png" alt="frp项目Github仓库"></p>
<h1 id="公网访问内网机器：基于frp的内网穿透"><a href="#公网访问内网机器：基于frp的内网穿透" class="headerlink" title="公网访问内网机器：基于frp的内网穿透"></a>公网访问内网机器：基于frp的内网穿透</h1><blockquote>
<p><strong>从公网中访问自己的私有设备向来是一件难事儿。</strong></p>
</blockquote>
<h2 id="1-为什么需要内网穿透？"><a href="#1-为什么需要内网穿透？" class="headerlink" title="1. 为什么需要内网穿透？"></a>1. 为什么需要内网穿透？</h2><h3 id="A-计算机网络"><a href="#A-计算机网络" class="headerlink" title="A. 计算机网络"></a>A. 计算机网络</h3><p>如何在自己的机器上访问另外一台机器？一台机器本身是一个独立的整体，可以直接访问本身所存储的文件。而如果现在有两台机器的话，机器A想要访问机器B上的文件，该怎么做？</p>
<p>事实上，当两台、乃至多台机器在一起的时候，就构成了一个以单台计算机为节点，以计算机和计算机之间的链接为边的网络，即计算机网络。我们访问另外一台计算机文件，就需要通过计算机网络来实现。</p>
<p>计算机网络中有一个比较重要的问题，就是如何标识每一台计算机。作为人，我们能很简单的知道，这是Jack Wang的Mac、那是Sarah的Windows、那是课题组公用的Linux，但是对于计算机来说，这是很难的。</p>
<p>所以类似于身份证能够识别不同的人一样，给组成计算机网络中的每一台设备一个编号，这个编号就作为每一台机器在网络中的标识，利用这个编号，能够找到需要访问的计算机。这个编号称为<code>IP地址</code></p>
<p><strong>所以，想要访问另外一台电脑，就需要知道对方的IP地址，然后才能访问。</strong></p>
<h3 id="B-私网间互相访问"><a href="#B-私网间互相访问" class="headerlink" title="B. 私网间互相访问"></a>B. 私网间互相访问</h3><p>IP地址根据最初的设定，分为ABC几类。直白的来说就是有一些IP地址是保留给局域网使用（私网），剩下的IP地址全球共享（公网）</p>
<ul>
<li>公网IP地址是不能重复的。</li>
<li><strong>保留给局域网使用的IP地址在不同的局域网间是可以重复的</strong>。例如：你家和我家可能都有<code>192.168.0.1</code>这个IP地址，而使用这个IP地址的设备在你的局域网中可能是你的手机，而在我的局域网中可能是我的PC。但是，<strong>保留给局域网使用的IP地址在同一个局域网内是不能重复的</strong>。例如：我家的<code>192.168.0.1</code>这个IP地址只可能被一部设备使用，要么是我的手机，要么是我的PC。</li>
</ul>
<p>因此，不同局域网内的IP地址可重复这一性质就导致了两台处于不同局域内机器互相连接可能存在问题：</p>
<ul>
<li>我们在办公室中使用的PC连接了办公室的网络，因此具有一个私网的IP地址，可能是<code>192.168.0.1</code>；我们的手机连接办公室的网络，因此也具有一个私网地址，可能是<code>192.168.0.2</code></li>
<li>我们需要连接的在家里的PC连接了家里的网络，因此具有一个私网的IP地址，可能是<code>192.168.0.2</code></li>
</ul>
<p>那么我们使用在办公室内的机器连接<code>192.168.0.2</code>这个地址的时候，我们本来想连接家里的PC，但是由于私网的IP地址可重复，我们可能会连接到同一局域网下的另外一台机器。</p>
<h3 id="C-借助公网设备作为跳板"><a href="#C-借助公网设备作为跳板" class="headerlink" title="C. 借助公网设备作为跳板"></a>C. 借助公网设备作为跳板</h3><p>解决问题的办法就是获得一个公网IP。因为世界上所有的网络在一起，构成了互联网，或者公网。公网IP在公网中是唯一的。所以不管我们处于哪个局域网，都可以访问到具有公网IP的机器。</p>
<p>所以解决方案就是：</p>
<ul>
<li>机器A和机器B都先连接到公网机器。</li>
<li>因为连接在建立后通信是双向的，所以在机器A、B连接到公网机器后，可以和公网机器双向通信</li>
<li>而后机器A把需要访问的机器B上的文件路径传给公网机器</li>
<li>公网机器把文件路径通知给机器B</li>
<li>机器B把文件传输给公网机器</li>
<li>公网机器再把文件传输给机器A</li>
</ul>
<p>这样利用公网机器作为跳板，就实现了处于两个私网的机器间的访问。这种解决方案称为<code>内网穿透</code>，即借助一个具有公网IP的服务器作为跳板，来让处于其他私网的设备访问到内网中的机器。这样做类似于把内网穿透到公网上，因此称为<code>内网穿透</code>.</p>
<h3 id="D-哪来的公网IP？"><a href="#D-哪来的公网IP？" class="headerlink" title="D. 哪来的公网IP？"></a>D. 哪来的公网IP？</h3><p>上面解决两个私网机器互相访问最核心的，就是要有一个具有公网IP的机器。而由于中国加入到互联网的时间比较晚，所以分到的公网IP就比较少。原来很早的时候，打电话免费就可以申请得到一个公网IP，但现在运营商因为IP地址短缺不给你分配公网IP地址。</p>
<p>如果花钱买的话，目前市场中常用的网络有移动宽带、联通宽带、电信宽带，不同的网络运营商提供业务办理不同，收取的费用标准也存在一定的不同。一般情况，申请公网IP大概需要1000-2000元，具体的收费标准还需要以当地各大网络运营商提供的具体解决方案为准。所以就很贵。</p>
<p>所以就出现了一些服务商，他们购买了公网IP，然后架设了公网机器，我们只需要购买这些服务商提供的跳板服务/穿透服务就行了。但是这样功能非常有限，只有跳板服务，可玩性和功能性不高。</p>
<p>因此，我们不如直接租一个具有公网IP的服务器，然后自己架设跳板服务/穿透服务即可</p>
<h2 id="2-frp搭建内网穿透"><a href="#2-frp搭建内网穿透" class="headerlink" title="2. frp搭建内网穿透"></a>2. frp搭建内网穿透</h2><h3 id="A-什么是frp"><a href="#A-什么是frp" class="headerlink" title="A. 什么是frp"></a>A. 什么是frp</h3><p>既然我们需要进行内网穿透，肯定就需要有一个程序帮助我们去完成公网、私网机器连接的建立，公网机器上消息的转发等等功能，这个软件就是<code>frp</code>。</p>
<p>简单地说，<code>frp</code>就是一个反向代理软件，它体积轻量但功能很强大。利用他就可以实现反向穿透，即可以使处于内网或防火墙后的设备对外界提供服务。</p>
<p>它支持<code>HTTP</code>、<code>TCP</code>、<code>UDP</code>等众多协议。我们今天使用的仅限于<code>TCP</code>和<code>UDP</code>，所以使用<code>frp</code>是足够了</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717175118145.png" alt="frpc项目的Github主页" style="zoom: 25%;"></p>
<h3 id="B-机器配置信息"><a href="#B-机器配置信息" class="headerlink" title="B. 机器配置信息"></a>B. 机器配置信息</h3><p>既然<code>frp</code>需要建立公网机器和私网机器之间的连接，因此frp实际上是两个程序：</p>
<ul>
<li><code>frpc</code>：即<code>frp</code>客户端（<code>client</code>），运行在需要被穿透到公网上的机器，即将来被远程访问的机器</li>
<li><code>frps</code>：即<code>frp</code>服务端（<code>server</code>），运行在具有公网IP的机器上</li>
</ul>
<p>而在运行的时候，<code>frp</code>是根据配置文件中的描述来运行的。</p>
<p>因此，我们未来需要在公网机器和被远程访问的机器上分别下载<code>frp</code>程序，并编写配置文件。</p>
<p>这里我的配置是：</p>
<ul>
<li>公网机器是一台腾讯云服务器，后面就简称为服务器<ul>
<li>公网地址是<code>81.68.123.84</code></li>
<li>用户名是<code>lighthouse</code></li>
<li>主机名是<code>VM-4-7-ubuntu</code></li>
</ul>
</li>
<li>需要被穿透的机器（被其他私网机器访问的机器，即家里的机器）称为PC端<ul>
<li>私网IP地址不重要</li>
<li>用户名是<code>jack</code></li>
<li>主机名是<code>jack-Alienware-Aurora-R13</code></li>
</ul>
</li>
</ul>
<h3 id="C-下载frp"><a href="#C-下载frp" class="headerlink" title="C. 下载frp"></a>C. 下载frp</h3><h4 id="1-服务器下载frps"><a href="#1-服务器下载frps" class="headerlink" title="1) 服务器下载frps"></a>1) 服务器下载frps</h4><p>首先连接进服务器下载<code>frp</code>服务端</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ssh</span> lighthouse@81.68.123.84
<span class="token function">mkdir</span> -p ~/opt/frps
<span class="token function">cd</span> opt/frps
<span class="token function">wget</span> -c https://github.com/fatedier/frp/releases/download/v0.51.0/frp_0.51.0_linux_amd64.tar.gz
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p>注意，不同CPU架构下载的链接是不同的，我的服务器是<code>Intel</code>的<code>CPU</code>，所以是<code>x86_64</code>架构，下载<code>amd64</code>版本的程序。你需要根据自己的服务器的架构去下载。</p>
<p>所有版本的下载地址：<a target="_blank" rel="noopener" href="https://github.com/fatedier/frp/releases">https://github.com/fatedier/frp/releases</a></p>
</blockquote>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717180438414.png" alt="下载完成"></p>
<p>然后解压</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">tar</span> xzvf frp_0.51.0_linux_amd64.tar.gz
<span class="token function">ls</span> -al frp_0.51.0_linux_amd64
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717181140244.png" alt="可以看到，下载的程序是包含客户端和服务端的"></p>
<p>因为目前是在服务器上配置<code>frp</code>服务端，因此删去不需要的客户端程序：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">rm</span> frp_0.51.0_linux_amd64/frpc*
<span class="token function">ls</span> -al frp_0.51.0_linux_amd64
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717181413449.png" alt="服务器上只剩下frp服务端"></p>
<h4 id="2-PC端下载frpc"><a href="#2-PC端下载frpc" class="headerlink" title="2) PC端下载frpc"></a>2) PC端下载frpc</h4><p>步骤和服务器上下载<code>frps</code>基本是一样的，这不过这里要删除的是<code>frp</code>服务端</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">mkdir</span> -p ~/opt/frpc
<span class="token function">cd</span> ~/opt/frpc
<span class="token function">wget</span> -c https://github.com/fatedier/frp/releases/download/v0.51.0/frp_0.51.0_linux_amd64.tar.gz
<span class="token function">tar</span> xzvf frp_0.51.0_linux_amd64.tar.gz
<span class="token function">rm</span> frp_0.51.0_linux_amd64/frps*
<span class="token function">ls</span> -al frp_0.51.0_linux_amd64
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717183219182.png" alt="PC端只剩下frp客户端"></p>
<h3 id="D-配置frp"><a href="#D-配置frp" class="headerlink" title="D. 配置frp"></a>D. 配置frp</h3><p>类似于下载，配置<code>frp</code>也是分为PC端配置和服务器配置。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717194201962.png" alt="frp工作流程：以SSH登录为例" style="zoom:33%;"></p>
<p><code>frp</code>的工作流程如上图。假设现在远程主机需要远程登录PC机，那么实际上就是远程主机使用PC机上的SSH程序，即访问PC机上的<code>22</code>端口。</p>
<p>所以，当远程主机的一个<code>ssh</code>请求来了之后：</p>
<ul>
<li>远程主机的<code>ssh</code>请求打到服务器上的<code>10001</code>端口</li>
<li><code>10001</code>端口被<code>frps</code>监听，检测到有一个<code>ssh</code>请求后，将其通过本机的<code>6006</code>端口发出去</li>
<li>PC端的<code>frpc</code>接收到远程主机发来的<code>ssh</code>请求后，将其转发到本机的<code>22</code>端口</li>
<li>远程主机的<code>ssh</code>请求最终被监听PC端<code>22</code>端口的服务程序处理</li>
</ul>
<p>因此：</p>
<ul>
<li>PC端（<code>frp</code>客户端）需要指定，将自己的22号端口映射为服务器（<code>frp</code>服务端）的<code>10001</code>端口，或者说需要将服务器的<code>10001</code>端口的流量转发到PC端的<code>22</code>端口</li>
<li>服务器（<code>frp</code>服务端）需要指定PC机（<code>frp</code>客户端）需要通过哪个端口和服务器（<code>frp</code>服务端）通信</li>
</ul>
<p>可以看到，<code>frp</code>其实最重要的就是转发了远程主机发来的数据包，因此<code>frp</code>实际上工作在传输层。稍后我们在配置中就能看到，在<code>frp</code>的配置文件中，我们需要指定服务器的哪个端口的哪种数据包需要转发到PC端的哪一个端口。</p>
<p>针对上面举的<code>SSH</code>的例子，<code>SSH</code>服务本质上是一个文本传输服务，只不过进行了加密，因此<code>SSH</code>的数据包使用的是<code>TCP</code>协议。后面穿透不同的服务的时候需要指定数据包的类型</p>
<h4 id="1-服务器配置"><a href="#1-服务器配置" class="headerlink" title="1) 服务器配置"></a>1) 服务器配置</h4><p>服务器端要配置的，实际上就只有<code>bind_port</code>。因为<code>listen_port</code>是需要<code>frp</code>客户端指定的</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ssh</span> lighthouse@81.68.123.84
<span class="token function">cd</span> ~/opt/frps/frp_0.51.0_linux_amd64
vim frps.ini
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>将其中的<code>bind_port</code>修改为<code>6006</code>，这里是为了和上面的图配合起来，你也可以改成自己喜欢的。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cat</span> frps.ini
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717195723395.png" alt="frp服务端将监听服务器的6006端口来接收frp客户端的消息"></p>
<p>服务器端的配置就结束了，最后得到的配置文件为：</p>
<pre class="line-numbers language-yaml"><code class="language-yaml"><span class="token comment" spellcheck="true"># frps.ini，即服务端上的配置</span>
<span class="token punctuation">[</span>common<span class="token punctuation">]</span>
bind_port = 6006
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h4 id="2-PC端配置"><a href="#2-PC端配置" class="headerlink" title="2) PC端配置"></a>2) PC端配置</h4><p>客户端的配置比服务端配置要复杂些，因为要指定远程端口的某种流量转发到本地的某个端口。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cd</span> ~/opt/frpc/frp_0.51.0_linux_amd64
vim frpc.ini
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>按照上面介绍的</p>
<ul>
<li>首先设置PC端的<code>frp</code>客户端连接到的<code>frp</code>服务端的IP地址为服务器地址，即设置<code>server_addr=81.68.123.84</code></li>
<li>然后设置PC端的<code>frp</code>客户端连接道<code>frp</code>服务端监听的端口，即设置<code>server_port=6006</code></li>
</ul>
<p>接着编写一个新的规则</p>
<ul>
<li>规则名字可以随便给，这里直接就是<code>Aurora-SSH</code></li>
<li>服务端需要监听服务端的<code>10001</code>端口</li>
<li>服务端转发的数据包类型就是<code>tcp</code></li>
<li>服务端转发的数据包的目的地址就是本机地址<code>127.0.0.1</code></li>
<li>服务端转发的数据包的目的端口就是<code>22</code>端口</li>
</ul>
<p>最后得到的总配置文件为</p>
<blockquote>
<p>注意，为了避免你的内网机器被穿透到我的服务器上，你需要修改一下<code>server_addr</code>，免得到时候我能登录你的内网机器\^_\^</p>
</blockquote>
<pre class="line-numbers language-yaml"><code class="language-yaml"><span class="token comment" spellcheck="true"># frpc.ini，即PC端上的配置</span>
<span class="token punctuation">[</span>common<span class="token punctuation">]</span>
server_addr = 81.68.123.84
server_port = 6006

<span class="token punctuation">[</span>Aurora<span class="token punctuation">-</span>SSH<span class="token punctuation">]</span>
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 10001
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>所以总的来说就是：将<code>81.68.123.84</code>服务器上的<code>10001</code>端口接收到的<code>tcp</code>数据包转发到本地的<code>22</code>端口，并且通过服务器的<code>6006</code>端口进行通信</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717200919024.png" alt="服务端配置文件"></p>
<h3 id="E-启动frp"><a href="#E-启动frp" class="headerlink" title="E. 启动frp"></a>E. 启动frp</h3><p>首先启动服务器端的<code>frps</code></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ssh</span> lighthouse@81.68.123.84
<span class="token function">cd</span> ~/opt/frps/frp_0.51.0_linux_amd64
./frps -c frps.ini<span class="token operator">|</span> <span class="token function">tee</span> -a frps.log
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>这里把命令行输出到信息备份一份到<code>frps.log</code>文件中</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717203531664.png" alt="启动frp服务端"></p>
<p>然后启动PC端的<code>frpc</code></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cd</span> ~/opt/frps/frp_0.51.0_linux_amd64
./frpc -c frpc.ini <span class="token operator">|</span> tess -a frpc.log
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717203708997.png" alt="启动frp客户端"></p>
<p>启动成功后，再检查一下服务器上的<code>frps</code>，发现已经接收到了<code>frpc</code>客户端发来的规则，开始监听端口<code>10001</code>了</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717203840954.png" alt="服务器上的frps开始监听端口10001"></p>
<h3 id="F-验证"><a href="#F-验证" class="headerlink" title="F. 验证"></a>F. 验证</h3><p>最后验证一下能否远程登录到PC端。</p>
<p>这里直接就在PC端下<code>SSH</code>连接服务器的<code>10001</code>端口，因为登录的是PC端的用户，所以用户名还需要是PC端的用户名，只不过此时<code>SSH</code>服务器地址已经变成了服务器的地址</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">ssh</span> jack@81.68.123.84 -p 10001
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717204158466.png" alt="成功登陆到PC端"></p>
<h2 id="3-番外：PC端开机自运行frpc"><a href="#3-番外：PC端开机自运行frpc" class="headerlink" title="3. 番外：PC端开机自运行frpc"></a>3. 番外：PC端开机自运行frpc</h2><p>最后，服务器因为我们随时可以链接进去，所以使用<code>tmux</code>开一个<code>session</code>，保持<code>frps</code>运行即可。但是PC端则需要我们事先手动运行<code>frpc</code>。</p>
<p>如果我们运行了一些命令导致PC端重启了，那么再次启动的时候就会导致服务丢失。所以，我们希望PC端在重新启动的时候能够自动运行<code>frpc</code>。</p>
<p>为此，我们使用Systemd来新建一个服务。关于Systemd后面会写点文章介绍，这里就先用吧。</p>
<blockquote>
<p>Systemd介绍：</p>
<p>Systemd是一个用于Linux系统的系统和服务管理器。它被设计为替代传统的SysV初始化系统（init）和System V启动脚本，并提供了更先进的功能和特性。</p>
<p>Systemd的目标是改进系统的启动速度、效率和可靠性，并提供更好的系统管理和服务控制功能。它引入了一种并行启动机制，可以同时启动系统中的多个服务，加快系统启动时间。此外，Systemd还支持基于套接字激活的服务，可以在需要时按需启动或停止服务，提高系统资源利用率。</p>
<p>Systemd还引入了一种统一的单元文件（unit files）格式，用于描述系统服务、设备、挂载点等。这些单元文件位于<code>/etc/systemd/system/</code>和<code>/usr/lib/systemd/system/</code>等目录下，通过这些单元文件，可以对系统的各个方面进行配置和管理。</p>
<p>Systemd提供了一系列的命令行工具，用于管理和控制系统和服务，例如<code>systemctl</code>命令用于启动、停止、重启和管理系统服务，<code>journalctl</code>命令用于查看系统日志。</p>
<p>总的来说，Systemd是一个现代化的系统和服务管理器，它在Linux系统中扮演着重要的角色，改进了系统的启动和管理方式，提供了更好的性能和功能。它已经成为许多主流Linux发行版的默认初始化系统。</p>
</blockquote>
<h3 id="A-编写启动脚本"><a href="#A-编写启动脚本" class="headerlink" title="A. 编写启动脚本"></a>A. 编写启动脚本</h3><p>首先编写一个脚本来运行<code>frpc</code></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cd</span> ~/opt/frpc
<span class="token comment" spellcheck="true"># 创建一个软连接，方便后面更新版本</span>
<span class="token function">ln</span> -s frp_0.51.0_linux_amd64 bin
vim start_frpc.sh
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>内容如下：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token shebang important">#! /bin/bash</span>

run_folder<span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">cd</span> <span class="token punctuation">$(</span>dirname $0<span class="token variable">)</span></span> <span class="token operator">&amp;&amp;</span> <span class="token function">pwd</span> <span class="token operator">||</span> exit<span class="token punctuation">)</span>
bin_folder<span class="token operator">=</span><span class="token string">"<span class="token variable">${run_folder}</span>/bin"</span>

<span class="token function">cd</span> <span class="token variable">${bin_folder}</span> <span class="token operator">&amp;&amp;</span> <span class="token variable">${bin_folder}</span>/frpc -c <span class="token variable">${bin_folder}</span>/frpc.ini  2<span class="token operator">></span><span class="token operator">&amp;</span>1 <span class="token operator">|</span> <span class="token function">tee</span> -a <span class="token variable">${run_folder}</span>/frpc.log
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>运行这个脚本就可以一键化直接运行<code>frpc</code>了</p>
<pre class="line-numbers language-bash"><code class="language-bash">chmox 755 start_frpc.sh
./start_frpc.sh
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230717210010336.png" alt="脚本启动frpc"></p>
<h3 id="B-开机运行启动脚本"><a href="#B-开机运行启动脚本" class="headerlink" title="B. 开机运行启动脚本"></a>B. 开机运行启动脚本</h3><p>编写完了开机脚本还不行，还需要开机就运行这个脚本。后面就是通过Systemd来设置开机运行这个脚本了。开机自运行的Systemd任务需要放在<code>/usr/lib/systemd/system</code>目录下</p>
<p>首先新建一个Systemd任务，名字为<code>frpc-client</code>，而后创建软连接方便修改</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cd</span> ~/opt/frpc
<span class="token function">sudo</span> <span class="token function">touch</span> /etc/systemd/system/frp-client.service
<span class="token function">ln</span> -s /etc/systemd/system/frp-client.service ./
<span class="token function">sudo</span> vim frp-client.service
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>然后编写内容如下：</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 这个部分定义了服务的基本信息, 这里是一个启动frpc的服务</span>
<span class="token punctuation">[</span>Unit<span class="token punctuation">]</span>
Description<span class="token operator">=</span>开机自动运行frpc任务
<span class="token comment" spellcheck="true"># 必须要在网络启动之后才能运行, 因为Systemd是多线程运行服务的</span>
After<span class="token operator">=</span>network.target
<span class="token comment" spellcheck="true"># 同时该任务以来网络, 因此声明启动网络服务是该服务的依赖</span>
Wants<span class="token operator">=</span>network.target



<span class="token comment" spellcheck="true"># 这个部分定义了服务的运行参数和行为</span>
<span class="token punctuation">[</span>Service<span class="token punctuation">]</span>
<span class="token comment" spellcheck="true"># 指定了服务的类型，这里是simple，表示是一个简单的服务，即执行指定的命令或脚本。</span>
Type<span class="token operator">=</span>simple
<span class="token comment" spellcheck="true"># 指定了运行服务的用户，这里是jack，表示以jack的身份运行服务</span>
User<span class="token operator">=</span>jack
<span class="token comment" spellcheck="true"># 无论是由于错误、异常退出还是手动停止，只要服务退出，它都应该立即重新启动。这确保了服务的持续性，即使出现问题导致服务停止，Systemd也会自动重新启动它，以恢复服务的正常运行。指定为always确保了下面的脚本会一直运行</span>
Restart<span class="token operator">=</span>always
<span class="token comment" spellcheck="true"># 指定了服务在重启之前的等待时间</span>
RestartSec<span class="token operator">=</span>5s
<span class="token comment" spellcheck="true"># 指定了服务可以打开的最大文件描述符数，这里是1048576</span>
LimitNOFILE<span class="token operator">=</span>1048576
<span class="token comment" spellcheck="true"># 指定了要运行的命令或脚本的绝对路径</span>
ExecStart<span class="token operator">=</span>/bin/bash /home/jack/opt/frpc/start_frpc.sh
<span class="token comment" spellcheck="true"># 服务重新加载（reload）时执行的命令或脚本。</span>
ExecReload<span class="token operator">=</span>/bin/bash /home/jack/opt/frpc/start_frpc.sh

<span class="token comment" spellcheck="true"># 这个部分定义了服务的安装和启动配置</span>
<span class="token punctuation">[</span>Install<span class="token punctuation">]</span>
<span class="token comment" spellcheck="true"># 下面这段不能少, 强制要求了在用户登录前运行启动脚本</span>
WantedBy <span class="token operator">=</span> multi-user.target
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>然后把这个启用这个任务</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 因为新建了一个开机启动任务，所以重新加载一下systemd守护进程</span>
<span class="token function">sudo</span> systemctl daemon-reload
<span class="token function">sudo</span> systemctl <span class="token function">enable</span> frp-client.service
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230718095001157.png" alt="启用frpc任务"></p>
<p>然后查看一下这个任务的状态</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">sudo</span> systemctl status frp-client.service
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>因为目前只是加载了这个任务（表示启动时将会运行这个任务），而由于上次启动时还没运行这个任务，所以显示的是dead。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230718095044940.png" alt="查看frpc任务"></p>
<p>紧着这，启动一下这个服务</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">sudo</span> systemctl start frpc-client.service
<span class="token function">sudo</span> systemctl status frpc-client.service
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230718095206222.png" alt="启动frpc任务"></p>
<p>接下来重启一下PC端就行了，此时这个任务就会被运行</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">sudo</span> <span class="token function">reboot</span>
<span class="token comment" spellcheck="true"># 重启后检查一下任务状态</span>
<span class="token function">sudo</span> systemctl status frp-client.service
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20230718095857535.png" alt="开机运行frpc，赢！"></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack Wang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jackwang0107.github.io/2023/07/17/gong-wang-fang-wen-nei-wang-ji-qi-frp/">https://jackwang0107.github.io/2023/07/17/gong-wang-fang-wen-nei-wang-ji-qi-frp/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack Wang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">
                                    <span class="chip bg-color">内网穿透</span>
                                </a>
                            
                                <a href="/tags/frp/">
                                    <span class="chip bg-color">frp</span>
                                </a>
                            
                                <a href="/tags/Linux%E5%AE%9E%E7%94%A8%E6%8A%80%E5%B7%A7/">
                                    <span class="chip bg-color">Linux实用技巧</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2023/07/20/linux-you-jian-shou-fa-you-jian-er-san-shi/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/Mu4e-Email-Client-for-Linux.png" class="responsive-img" alt="Linux命令行邮件收发：邮件通信与snail">
                        
                        <span class="card-title">Linux命令行邮件收发：邮件通信与snail</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文介绍了邮件通信模型的相关知识，从而介绍了如何在Linux上使用snail收发邮件
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2023-07-20
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Linux%E5%AE%9E%E7%94%A8%E6%8A%80%E5%B7%A7/" class="post-category">
                                    Linux实用技巧
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Linux/">
                        <span class="chip bg-color">Linux</span>
                    </a>
                    
                    <a href="/tags/mail/">
                        <span class="chip bg-color">mail</span>
                    </a>
                    
                    <a href="/tags/sendmail/">
                        <span class="chip bg-color">sendmail</span>
                    </a>
                    
                    <a href="/tags/mailx/">
                        <span class="chip bg-color">mailx</span>
                    </a>
                    
                    <a href="/tags/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">
                        <span class="chip bg-color">内网穿透</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2023/07/16/linux-zhou-qi-ren-wu/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/9f27cb5c78ed96868b74263320fc5dcd.png" class="responsive-img" alt="Linux周期任务：at和crontab">
                        
                        <span class="card-title">Linux周期任务：at和crontab</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文主要介绍了Linux中的crontab和at
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2023-07-16
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Linux%E5%AE%9E%E7%94%A8%E6%8A%80%E5%B7%A7/" class="post-category">
                                    Linux实用技巧
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Linux/">
                        <span class="chip bg-color">Linux</span>
                    </a>
                    
                    <a href="/tags/at/">
                        <span class="chip bg-color">at</span>
                    </a>
                    
                    <a href="/tags/crontab/">
                        <span class="chip bg-color">crontab</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: JackWang&#39;s Blog<br />'
            + '文章作者: Jack Wang<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>


<script src="https://cdn.bootcss.com/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
    MathJax.Hub.Config({
        tex2jax: {inlineMath: [['$', '$'], ['\\(', '\\)']]}
    });
</script>



    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <a href="/about" target="_blank">Jack Wang</a>
            <!-- |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a> -->
            <!-- |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">603.8k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "11";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp"><img src="/medias/icp.png"
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">陕ICP备2021014294号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jackwang0108" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2232123545@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2232123545" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2232123545" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
